package thread.producer;

import java.util.ArrayList;
import java.util.List;

public class WareHouse {
    private ArrayList<String> list = new ArrayList<>();

    public synchronized void add(){
        if (list.size() < 20){
            list.add("a");
        }else {
            try {
                this.notifyAll();
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public  void get(){
        //这里this（house）、list都可以
        synchronized(this) {
            if (list.size() > 0) {

                list.remove(0);

            } else {
                try {
                    this.notifyAll();
                    this.wait();//把锁解开，cpu让出去，该（consumer）线程阻塞，其他线程访问
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
